<?php

namespace app\http\middleware;

use think\DB;

class CheckSmsCode
{
    public function handle($request, \Closure $next) {
        if ($request->post('smsCode') == '') {
            return json(['code' => 1004, 'msg' => lang(1004)]);
        }
//
//        if (!session('?smsCode.code')) {
//            return json(['code' => 1007,'msg'=>lang(1007)]);
//        }
//
//        if ($request->post('smsCode') != session('smsCode.code')) {
//            return json(['code' => 1005,'msg'=>lang(1005)]);
//        }
//
//        if ($request->post('cellphone') != session('smsCode.phone')) {
//            return json(['code' => 1006,'msg'=>lang(1006)]);
//        }
//
//        session('smsCode',null);

        $dataRow = DB::name('sms_code')->where('phone', $request->post('cellphone'))->where('time', '>=', \tool\Date::date(time() - 900))->find();

        if (!$dataRow) {
            return json(['code' => 1007, 'msg' => lang(1007)]);
        }

        if ($dataRow['code'] != $request->post('smsCode')) {
            return json(['code' => 1005, 'msg' => lang(1005)]);
        }

        //DB::name('sms_code')->where('phone', $request->post('cellphone'))->delete();

        return $next($request);
    }
}
